import pytz
from zoneinfo import ZoneInfo
from datetime import datetime, timezone

from odoo import fields, models, _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT


class ReportWorkorderScrapSumWizard(models.TransientModel):
    _name = 'report.workorder.scrap.sum.wizard'
    _description = 'Report Workorder Scrap Sum Wizard'

    product_id = fields.Many2one('product.product', string='Product')
    start_date = fields.Datetime('Start Date')
    end_date = fields.Datetime('End Date')

    def action_download(self):
        # tz = self.env.context.get('tz')
        #
        # right_tz = pytz.timezone(tz)
        # start_date = right_tz.localize(fields.Datetime.to_datetime(self.start_date).replace(tzinfo=None))
        # start_date = start_date.astimezone(pytz.utc)
        #
        # end_date = right_tz.localize(fields.Datetime.to_datetime(self.end_date).replace(tzinfo=None))
        # end_date = end_date.astimezone(pytz.utc)

        min_start_date = min(self.start_date, self.end_date)
        max_end_date = max(self.start_date, self.end_date)

        return {
            'name': _('Workorder Scrap Sum Report'),
            'view_mode': 'tree',
            'res_model': 'workorder.scrap.sum.report',
            'type': 'ir.actions.act_window',
            'context': {
                'product_id': self.product_id.id,
                'start_date': min_start_date,
                'end_date': max_end_date
            },
        }
